#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:Charmander

import unittest

import paramunittest

from Advisement_player import readConfig as readConfig
from Advisement_player.common import common
from Advisement_player.common import configHttp as ConfigHttp
from Advisement_player.common import log

login_xls = common.get_xls('testCase.xlsx', 'login')
configHttp = ConfigHttp.ConfigHttp()
localReadConfig = readConfig.ReadConfig()
info = {}

@paramunittest.parametrized(*login_xls)
class Login(unittest.TestCase):
    def setParameters(self,case_name,method,token,mobile,pwd,result,code,msg):
        self.case_name = str(case_name)
        self.method = str(method)
        self.token = str(token)
        self.mobile = str(mobile)
        self.pwd = str(pwd)
        self.result = str(result)
        self.code = str(code)
        self.msg = str(msg)
        self.return_json = None
        self.info = None

    def description(self):
        return self.case_name

    def setUp(self):
        self.log = log.MyLog.get_log()
        self.logger = self.log.get_logger()
        print(self.case_name + '测试开始前准备')

    def testLogin(self):

        #set url
        self.url ='/v1/user/signIn'
        configHttp.set_url(self.url)
        print('输入的url')

        #set headers
        if self.token == '0':
            appid = localReadConfig.get_headers('app-id')
            deviceid = localReadConfig.get_headers('device-id')
            devicetype = localReadConfig.get_headers('device-type')
            osversion = localReadConfig.get_headers('os-version')
            appversion = localReadConfig.get_headers('app-version')
            header = {'app-id':str(appid),
                      'device-id':str(deviceid),
                      'device-type':str(devicetype),
                      'os-version':str(osversion),
                      'app-version':str(appversion)}
            configHttp.set_headers(header)
        else:
            token = None
            header = {"token": str(token)}
            configHttp.set_headers(header)
        print('设置header(token等)' )

        #set body
        body = {'mobile':self.mobile,'pwd':self.pwd}
        configHttp.set_data((body))
        print('发送请求的参数' )

        #test interface
        self.return_json = configHttp.post()
        method = str(self.return_json.request)[int(str(self.return_json.request).find('['))+1:int(str(self.return_json.request).find(']'))]
        print(method)

    def tearDown(self):
        self.info = self.return_json.json()

        info = self.info
        if info['success'] == True:
            BIT_TOKEN = common.get_value_from_return_json(info, 'token')
            BIT_UID = common.get_value_from_return_json(info, 'id')
            COMPANY_ID = common.get_value_from_return_json(info, 'companyId')
            localReadConfig.set_headers('BIT-TOKEN',BIT_TOKEN)
            localReadConfig.set_headers('BIT-UID',BIT_UID)
            localReadConfig.set_headers('COMPANY-ID',COMPANY_ID)
        else:
            pass
        self.log.build_case_line(self.case_name, self.info['errorCode'],self.info['errorMsg'])
        print('测试结束')




